package fx.samples.checkboxsample;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class CheckboxSample extends Application {

	Rectangle rect = new Rectangle(90, 30);
	final String[] names = new String[]{"Security", "Project", "Chart"};
	final Image[] images = new Image[names.length];
	final ImageView[] icons = new ImageView[names.length];
	final CheckBox[] cbs = new CheckBox[names.length];

	public static void main(final String[] args) {
		launch(args);
	}

	@Override
	public void start(final Stage stage) {
		final Scene scene = new Scene(new Group());
		stage.setTitle("Checkbox Sample");
		stage.setWidth(230);
		stage.setHeight(120);

		rect.setArcHeight(10);
		rect.setArcWidth(10);
		rect.setFill(Color.rgb(41, 41, 41));

		for (int i = 0; i < names.length; i++) {
			final Image image = images[i] =
					new Image(getClass().getResourceAsStream(names[i] + ".png"));
			final ImageView icon = icons[i] = new ImageView();
			final CheckBox cb = cbs[i] = new CheckBox(names[i]);
			cb.selectedProperty().addListener((ChangeListener<Boolean>) (ov, old_val, new_val) -> icon.setImage(new_val ? image : null));
		}

		final VBox vbox = new VBox();
		vbox.getChildren().addAll(cbs);
		vbox.setSpacing(5);

		final HBox hbox = new HBox();
		hbox.getChildren().addAll(icons);
		hbox.setPadding(new Insets(0, 0, 0, 5));

		final StackPane stack = new StackPane();

		stack.getChildren().add(rect);
		stack.getChildren().add(hbox);
		StackPane.setAlignment(rect, Pos.TOP_CENTER);

		final HBox root = new HBox();
		root.getChildren().add(vbox);
		root.getChildren().add(stack);
		root.setSpacing(40);
		root.setPadding(new Insets(20, 10, 10, 20));

		((Group) scene.getRoot()).getChildren().add(root);

		stage.setScene(scene);
		stage.show();
	}
}
